<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height: 60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addRoleList"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column label="序号" width="120" type="index" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="scoped">
              <el-button size="small" type="success" @click="assignPemission(scoped.row.id)">分配权限</el-button>
              <el-button
                size="small"
                type="primary"
                @click="editRole(scoped.row.id)"
              >编辑</el-button>
              <el-button
                size="small"
                type="danger"
                @click="delRole(scoped.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :total="total"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog title="编辑弹层" :visible="showDialog" @close="close">
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small">取消</el-button>
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- //分配权限点弹框 -->
    <assign-pemission
      :show-assign-dialog="showAssignDialog"
      :cur-role-id="curRoleId"
      @close-dialog="showAssignDialog=false"
    />
  </div>
</template>

<script>
import {
  getRoleList,
  deleteRole,
  addRole,
  getRoleDetail,
  updateRole
} from '@/api/setting.js'
import assignPemission from './component/assign-permission'
export default {
  components: {
    assignPemission
  },
  data() {
    return {
      roleList: [], // 角色列表
      params: {
        page: 1, // 页码
        pageSize: 10 // 每页数据条数
      },
      total: 0, // 记录总数
      showDialog: false, // 控制弹框显示
      roleForm: {
        // 弹框数据
        name: '',
        description: ''
      },
      rules: {
        // 校验规则
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      },
      showAssignDialog: false, // 控制弹框显示
      curRoleId: ''
    }
  },
  mounted() {
    this.loadRoleList()
  },
  methods: {
    // 拉取 数据
    async loadRoleList() {
      const res = await getRoleList(this.params)
      this.roleList = res.data.rows
      this.total = res.data.total
    },
    pageChange(page) {
      // 使用当前页面 作为接口字段 拉取新的数据
      this.page = page // 更换页面
      // 拉取新的数据
      this.loadRoleList()
    },
    async delRole(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 调用删除接口
          await deleteRole(id)
          this.loadRoleList()
          // 提示用户
          this.$message.success('删除角色成功')
        })
        .catch()
    },
    addRoleList() {
      // 新增
      this.showDialog = true
    },
    close() {
      // 取消
      // 清空表单数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      // 关闭弹框
      this.showDialog = false
    },
    btnOk() {
      // 兜底校验
      this.$refs.roleForm.validate(async(valid) => {
        if (valid) {
          if (this.roleForm.id) {
            await updateRole(this.roleForm)
            this.showDialog = false
            this.loadRoleList()
          } else {
            await addRole(this.roleForm)
            this.loadRoleList()
            this.close()
          }
        }
      })
    },
    // 编辑功能
    async editRole(id) {
      this.showDialog = true
      const res = await getRoleDetail(id)
      this.roleForm = res.data
    },
    // 分配权限
    assignPemission(id) {
      this.curRoleId = id
      this.showAssignDialog = true
    }
  }
}
</script>

<style>
</style>
